package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.searchusers;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher;

import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;

/**
 * @author Renato Miceli
 */
public class SearchUsersFormPanel extends FormPanel {

	private static String NAME_FIELD = "name";

	private static String NAME_INSENSITIVE_FIELD = "nameInsensitive";
	
	private static String LOGIN_FIELD = "login";

	private static String EMAIL_FIELD = "email";

	public SearchUsersFormPanel() {

		this.setLabelWidth(100);
		this.setWidth(300);
		this.setHeight(100);
		this.setFrame(true);

		TextField regName = new TextField("Name", NAME_FIELD);
		this.add(regName, new AnchorLayoutData("90%"));

		Radio nameRadio = new Radio("NameInsensitive", NAME_INSENSITIVE_FIELD);
		this.add(nameRadio, new AnchorLayoutData("50%"));
		
		TextField regLogin = new TextField("Login", LOGIN_FIELD);
		this.add(regLogin, new AnchorLayoutData("90%"));
		

		TextField regEmail = new TextField("E-mail", EMAIL_FIELD);
		regEmail.setRegex(".+@.+\\.[a-z]+");
		regEmail.setRegexText("Invalid email");

		this.add(regEmail, new AnchorLayoutData("90%"));

		regName.focus();
	}

	/**
	 * {@inheritDoc}
	 */
	public UserMatcher parseForm() {
		UserMatcher userMatcher = new UserMatcher();
		Form form = this.getForm();
		String login = form.findField(LOGIN_FIELD).getValueAsString();
		userMatcher.setLogin(login.isEmpty() ? null : login);
		String email = form.findField(EMAIL_FIELD).getValueAsString();
		userMatcher.setEmail(email.isEmpty() ? null : email);
		String name = form.findField(NAME_FIELD).getValueAsString();
		userMatcher.setName(name.isEmpty() ? null : name);
		return userMatcher;
	}
}
